package com.naja.auth2clientfirst.support;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 拓展用户权限仓储层
 * @auther wangjianying
 * @date 2023/12/27 15:58
 */
public final class ExpandUserAuthoritiesRepository {
    private static volatile ExpandUserAuthoritiesRepository single;

    private ExpandUserAuthoritiesRepository() {
    }

    public static ExpandUserAuthoritiesRepository instance() {
        if (single == null) {
            synchronized (ExpandUserAuthoritiesRepository.class) {
                if (single == null) {
                    single = new ExpandUserAuthoritiesRepository();
                }
            }
        }
        return single;
    }

    private final Map<String, List<String>> userCache = new ConcurrentHashMap<>();

//    {
//        userCache.put("zhangsan",  new ArrayList<>(Arrays.asList("test1")));
//        userCache.put("lisi", new ArrayList<>(Arrays.asList("test1", "test2")));
//    }


    public List<String> findByName(String name) {
        if (!this.userCache.containsKey(name)) {
            this.userCache.put(name, new ArrayList<>());
        }
        return this.userCache.get(name);
    }

    public void add(String name,String ... authority) {
        if (!this.userCache.containsKey(name)) {
            this.userCache.put(name, new ArrayList<>(Arrays.asList(authority)));
        } else {
            this.userCache.get(name).addAll(Arrays.asList(authority));
        }
    }

}
